package com.ruoyi.graduate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.graduate.domain.entity.MsCarousel;
import com.ruoyi.graduate.domain.param.MsCarouselAddParam;
import com.ruoyi.graduate.domain.param.MsCarouselPageParam;
import com.ruoyi.graduate.domain.param.MsCarouselUpdataParam;
import com.ruoyi.graduate.domain.vo.MsCarouselPageVo;
import com.ruoyi.graduate.mapper.MsCarouselMapper;
import com.ruoyi.graduate.mapping.MsCarouselMapping;
import com.ruoyi.graduate.service.IMsCarouselService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class IMsCarouselServiceImpl extends ServiceImpl<MsCarouselMapper, MsCarousel>
implements IMsCarouselService {

    @Autowired
    private MsCarouselMapper msCarouselMapper;


    @Override
    public PageInfo lisstPage(MsCarouselPageParam param) {

        QueryWrapper qw = new QueryWrapper();
        qw.like(!StringUtils.isEmpty(param.getName()), "name", param.getName());

        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<MsCarousel> list = msCarouselMapper.selectList(qw);

        List<MsCarouselPageVo> page = MsCarouselMapping.INSTANCE.page(list);
        return new PageInfo<>(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addCarousel(MsCarouselAddParam param) {

        MsCarousel add = MsCarouselMapping.INSTANCE.add(param);
        int insert = msCarouselMapper.insert(add);

        return insert > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateCarousel(MsCarouselUpdataParam param) {

        MsCarousel update = MsCarouselMapping.INSTANCE.update(param);
        int i = msCarouselMapper.updateById(update);

        return i > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    @Override
    public AjaxResult homeVo() {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("is_enabled",0);
        qw.orderByDesc("create_time");

        List list = msCarouselMapper.selectList(qw);
        List homevo = MsCarouselMapping.INSTANCE.homevo(list);
        return AjaxResult.success(homevo);
    }
}
